package com.itheima.springbootmybatis.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.springbootmybatis.domain.Student;
import com.itheima.springbootmybatis.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student>
        implements StudentService {

    @Autowired
    private StudentMapper studentMapper ;

    /**
     * 批量插入
     * @param list
     * @return
     */
    @Override
    public Map<String ,Object> saveStudent(List<Student> list) {
        int size = list.size();
        int number = 2 ;
        Map<String,Object> map = new HashMap<>() ;
        for (int i = 0; i < size/number + 1 ; i++) {
           if(i == size / number && size % number == 0){
               break ;
           }
           int end  = (i + 1) * number ;
           if(end >= size){
               end = size ;
           }
            List<Student> studentList1 = list.subList(i*number, end);
          //  Boolean aBoolean = studentMapper.saveStudent(studentList1);
            map.put("i" + i,studentMapper.saveStudent(studentList1)) ;
            System.out.println("当前 -- + "+ i + "插入数据 + " + (end - i*number));
        }
            long kaishi  = System.currentTimeMillis();
      //  System.out.println("批量插入----- + " + number + "条数据");
       //     studentMapper.saveStudent(studentList);
            long end = System.currentTimeMillis();
            System.out.println((end - kaishi) + "毫秒");

        return map ;
    }
}
